﻿using System;
using System.IO;
using System.Xml.Serialization;
using PSE.Deployment.SetupBase;
using PSE.Deployment.Engine.Interfaces;
using Sinacor.Deployment.Setup.Configuration;
using Sinacor.Deployment.Setup.Main;
using Sinacor.Deployment.Setup.Parameter;
using Sinacor.Deployment.Setup.Uninstall.DataBase.Steps;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Groups;

namespace Sinacor.Deployment.Setup.Uninstall.DataBase
{
    public class DataBaseGroup : BaseSetupGroup
    {
        public Group Group { get; set; }

        public DataBaseGroup(IStepGroup parent, Group group)
            : base(parent)
        {
            this.Id = group.Id;
            this.Description = group.Description;

            Group = group;

            this.ExecutionFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs>(DataBaseGroup_ExecutionFinished);
            this.CancelBehavior = PSE.Deployment.Engine.Enums.StepGroupCancelBehavior.StopParent;
            this.UndoFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.UndoFinishedEventArgs>(DataBaseGroup_UndoFinished);

            using (StreamReader sr = new StreamReader(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\DataBaseScripts.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Configuration.DataBaseScripts));
                SetupMainFactory.GetInstance<SinacorMain>().Configuration.Scripts = (Configuration.DataBaseScripts)serializer.Deserialize(sr);
            }

            //Insere os passos para execução do script para as empresas selecionadas
            foreach (Company company in SetupMainFactory.GetInstance<SinacorMain>().Parameter.DataBase.Companies)
            {
                this.Steps.Add(new CompanyDropSchema(this, company, false));
            }

            //Insere passos fixos
            this.Steps.Add(new SystemDropSchema(this, false));

            this.ExecutionFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs>(DataBaseGroup_ExecutionFinished);
            this.UndoFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.UndoFinishedEventArgs>(DataBaseGroup_UndoFinished);
        }

        private void DataBaseGroup_UndoFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.UndoFinishedEventArgs e)
        {
            if (e.OriginalSource == this)
            {
                this.Group.Install = false;
                this.Group.Installed = false;
            }
        }

        private void DataBaseGroup_ExecutionFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs e)
        {
            if (e.OriginalSource == this)
            {
                this.Group.Install = false;
                this.Group.Installed = false;
            }
        }

        public static string GetProgressDescription(string command)
        {
            if (!string.IsNullOrEmpty(command))
            {
                if (command.Trim().Length > 50)
                {
                    return command.Trim().Substring(0, 50).ToUpper() + "...";
                }
                else
                {
                    return command.Trim().ToUpper();
                }
            }

            return command;
        }
    }
}
